﻿#region License Info

//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#endregion

using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;

namespace CronosII
{
    public partial class SettingsForm : Form
    {
        protected Properties.Settings settings;

        public SettingsForm(Properties.Settings settings)
        {
            InitializeComponent();

            this.settings = settings;
            pixelSizeUpDown.Value = settings.WorkspaceMinimumNodePixelSize;
            nodeSizeUpDown.Value = (int) (settings.WorkspaceNodeSize*100);
            acfLag0CheckBox.Checked = settings.ACFShowLag0;
            hintsCheckBox.Checked = settings.ShowHints;
            defaultDataDirTextBox.Text = settings.DefaultDataDirectory;

            if (settings.PluginList == null)
                settings.PluginList = new StringCollection();
            foreach (var s in settings.PluginList)
                pluginListBox.Items.Add(s);
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                // copy values back into settings
                settings.WorkspaceMinimumNodePixelSize = (int) pixelSizeUpDown.Value;
                settings.WorkspaceNodeSize = (double) nodeSizeUpDown.Value/100.0;
                settings.ACFShowLag0 = acfLag0CheckBox.Checked;
                settings.ShowHints = hintsCheckBox.Checked;
                settings.DefaultDataDirectory = defaultDataDirTextBox.Text;
                settings.PluginList = new StringCollection();
                foreach (var pi in pluginListBox.Items)
                {
                    var s = pi as string;
                    if (s != null)
                       settings.PluginList.Add(s);
                }
            }
        }

        private void defaultDataDirButton_Click(object sender, System.EventArgs e)
        {
            var fs = new FolderBrowserDialog();
            fs.SelectedPath = defaultDataDirTextBox.Text;
            if (fs.ShowDialog()==DialogResult.OK)
            {
                defaultDataDirTextBox.Text = fs.SelectedPath;
            }
        }

        private void addPlugin_Click(object sender, System.EventArgs e)
        {

            // get the file name
            var opendialog = new OpenFileDialog();

            opendialog.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            opendialog.Filter = "dll Files (*.dll)|*.dll" +
                                "|All Files (*.*)|*.*";
            opendialog.CheckFileExists = true;
            opendialog.Multiselect = true; // select multiple files to merge

            if (opendialog.ShowDialog() == DialogResult.OK)
                foreach (var s in opendialog.FileNames)
                    pluginListBox.Items.Add(s);
        }

        private void removePlugin_Click(object sender, System.EventArgs e)
        {
            if (pluginListBox.SelectedIndex != -1)
               pluginListBox.Items.RemoveAt(pluginListBox.SelectedIndex);
        }
    }
}
